<template>
  <view>
    <view class="my-apply">
      <navigator v-if="!(form_id && dataId)" :url="`/pages-content/dynamicFormDemo/list?form_id=${form_id}`"
        >我的申请></navigator
      >
    </view>
    <dynamicForm
      :formSchema="jsonData"
      @submit="onSubmit"
      :open_id="open_id"
      :disabled="form_id && dataId"
    ></dynamicForm>
  </view>
</template>

<script>
import { mapState } from 'vuex'
import dynamicForm from '../components/fe-dynamicform'
import { requestProtocol, domain } from '@/common/constant.js'
export default {
  data() {
    return {
      jsonData: {},
      form_id: '',
      dataId: '',
    }
  },
  components: {
    dynamicForm,
  },
  computed: {
    open_id() {
      return uni.getStorageSync('USERINFO').openId || uni.getStorageSync('USERINFO').id
    },
    ...mapState(['targetParksCode', 'currentPark']),
  },
  onLoad(options) {
    //console.log(options)
    const { form_id, dataId, title } = options
    this.form_id = form_id
    this.dataId = dataId
    title && uni.setNavigationBarTitle({ title })
    if (form_id && !dataId) {
      this.requestFormData()
    }
    if (form_id && dataId) {
      this.requestFormDetail(dataId)
    }
  },
  methods: {
    async requestFormData() {
      // const result = await this.$request.post(
      //   '/smallapp/formController/getFormJson',
      //   {
      //     open_id: 'oaRy-5WmzmM7QoUsPfHCQA71fdvM',
      //     form_id: '2021051200000004',
      //     park_code: '001001',
      //   },
      //   'application/x-www-form-urlencoded',
      //   '',
      //   this
      // )
      const [error, { data }] = await uni.request({
        url: `${requestProtocol}${domain}/smallapp/formController/getFormJson`,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          open_id: this.open_id,
          form_id: this.form_id,
          park_code: this.targetParksCode || (this.currentPark && this.currentPark.parkscode),
        },
        method: 'POST',
      })
      //console.log('aaaa', data)
      if (error) {
        console.error(error)
        return
      }
      this.jsonData = data
    },
    async submit(model) {
      const { publishFaceId, faceId, functionCode, submitURL, tblName, parkCode, titleColumn, titleColumnValue } =
        this.jsonData
      const [error, { data }] = await uni.request({
        url: `${requestProtocol}${domain}/smallapp/formController/ajaxProxy`,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          mobile: '1',
          publishFaceId,
          faceId,
          functionCode,
          submitURL,
          enterpriseId: this.open_id,
          tblName,
          open_id: this.open_id,
          form_id: this.form_id,
          park_code: parkCode,
          titleColumn,
          titleColumnValue,
          ...model,
        },
        method: 'POST',
      })
      this.$tools.tip('提示', data.return_msg, () => uni.navigateBack())
    },
    onSubmit(data) {
      console.log('表单的数据', data)
      this.submit(data)
    },
    async requestFormDetail(dataId) {
      const [error, { data }] = await uni.request({
        url: `${requestProtocol}${domain}/smallapp/formController/getFormDataDetail`,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          open_id: this.open_id,
          faceId: this.form_id,
          dataId,
          park_code: this.targetParksCode || (this.currentPark && this.currentPark.parkscode) || '001001',
        },
        method: 'POST',
      })
      //console.log('aaaa', data)
      if (error) {
        console.error(error)
        return
      }
      console.log(data)
      this.jsonData = data
    },
  },
}
</script>

<style>
.my-apply {
  padding: 24rpx;
  padding-bottom: 0;
  text-align: right;
  color: #0080ff;
}
</style>
